package com.qububu.db.dao.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.qububu.common.notice.dto.NoticeInfoDto;
import com.qububu.common.notice.dto.NoticeInfoDto.NoticeType;
import com.qububu.db.dao.NoticeDao;

/**
 * @功能 与系统公告有关的数据库操作
 * */
@Repository
public class NoticeDaoImpl implements NoticeDao {

	private JdbcTemplate jdbcTemplate;
	
	public void setDataSource(DataSource dataSource) {
		this.jdbcTemplate = new JdbcTemplate(dataSource);
	}

	public List<NoticeInfoDto> getSiteNoticeList() {
		String sql = "select * from notice where notice_type = ?";
		List<NoticeInfoDto> siteNoticeList = null;
		
		List<Map<String, Object>> list = jdbcTemplate.queryForList(sql, NoticeType.SITE.toIntValue());
		siteNoticeList = new ArrayList<NoticeInfoDto>();
		NoticeInfoDto noticeInfo = null;
		for (Map<String, Object> map : list) {
			noticeInfo = new NoticeInfoDto((Long) map.get("notice_id"));
			
			noticeInfo.setNoticeContent(String.valueOf(map.get("notice_context")));
			noticeInfo.setNoticeSummaryText(String.valueOf(map.get("notice_summary_text")));
			noticeInfo.setNoticeCreateDttm(((Long) map.get("notice_create_dttm")).intValue() );
			noticeInfo.setNoticePicUrl(String.valueOf(map.get("notice_pic_url")));
			noticeInfo.setNoticeTitle(String.valueOf(map.get("notice_title")));
			noticeInfo.setNoticeType(NoticeType.getNoticeType((Integer) map.get("notice_type")));
			
			siteNoticeList.add(noticeInfo);
		}
		return siteNoticeList;
	}
	
	public List<NoticeInfoDto> getCommunityNoticeList() {
		String sql = "select * from notice where notice_type = ?";
		List<NoticeInfoDto> communityNoticeList = null;
		
		List<Map<String, Object>> list = jdbcTemplate.queryForList(sql, NoticeType.COMMUNITY.toIntValue() );
		communityNoticeList = new ArrayList<NoticeInfoDto>();
		NoticeInfoDto noticeInfo = null;
		for (Map<String, Object> map : list) {
			noticeInfo = new NoticeInfoDto((Long) map.get("notice_id"));
			
			noticeInfo.setNoticeContent(String.valueOf(map.get("notice_context")));
			noticeInfo.setNoticeSummaryText(String.valueOf(map.get("notice_summary_text")));
			noticeInfo.setNoticeCreateDttm(((Long) map.get("notice_create_dttm")).intValue() );
			noticeInfo.setNoticePicUrl(String.valueOf(map.get("notice_pic_url")));
			noticeInfo.setNoticeTitle(String.valueOf(map.get("notice_title")));
			noticeInfo.setNoticeType(NoticeType.getNoticeType((Integer) map.get("notice_type")));
			
			communityNoticeList.add(noticeInfo);
		}
		
		return communityNoticeList;
	}

	public NoticeInfoDto getNoticeInfo(int realNoticeId) {
		NoticeInfoDto noticeInfo = null;
		
		String sql = "select * from notice where notice_id = ?";
		Map<String, Object> noticeInfoMap = jdbcTemplate.queryForMap(sql, realNoticeId);
		
		noticeInfo = new NoticeInfoDto((Long)noticeInfoMap.get("notice_id"));
		noticeInfo.setNoticeContent(String.valueOf(noticeInfoMap.get("notice_context")));
		noticeInfo.setNoticeSummaryText(String.valueOf(noticeInfoMap.get("notice_summary_text")));
		noticeInfo.setNoticeCreateDttm(((Long) noticeInfoMap.get("notice_create_dttm")).intValue() );
		noticeInfo.setNoticePicUrl(String.valueOf(noticeInfoMap.get("notice_pic_url")));
		noticeInfo.setNoticeTitle(String.valueOf(noticeInfoMap.get("notice_title")));
		noticeInfo.setNoticeType(NoticeType.getNoticeType((Integer) noticeInfoMap.get("notice_type")));
		
		return noticeInfo;
	}

	public void doModifyNotice(NoticeInfoDto noticeInfoDto) {
		String sql = "update notice set notice_title = ?, notice_context = ?, notice_summary_text = ?, notice_pic_url = ?, notice_type = ? where notice_id = ? ";
		
		Object[] modifyNoticeInfo = {
				noticeInfoDto.getNoticeTitle(),
				noticeInfoDto.getNoticeContent(),
				noticeInfoDto.getNoticeSummaryText(),
				noticeInfoDto.getNoticePicUrl(),
				noticeInfoDto.getNoticeType().toIntValue(),
				noticeInfoDto.getNoticeId()
		};
		jdbcTemplate.update(sql, modifyNoticeInfo);
	}

	
	public boolean doDeleteNoticeById(int realNoticeId) {
		String sql = "delete from notice where notice_id = ? ";
		jdbcTemplate.update(sql, realNoticeId);
		
		return true;
	}

	public void doAddNoticeInfo(NoticeInfoDto noticeInfoDto) {
		String sql = "insert into notice(notice_title, notice_context, notice_summary_text, notice_pic_url, notice_create_dttm, notice_type) values(?, ?, ?, ?, ?, ?)";
		Object[] addNoticeInfo = {
				noticeInfoDto.getNoticeTitle(),
				noticeInfoDto.getNoticeContent(),
				noticeInfoDto.getNoticeSummaryText(),
				noticeInfoDto.getNoticePicUrl(),
				noticeInfoDto.getNoticeCreateDttm(),
				noticeInfoDto.getNoticeType().toIntValue()
		};
	
		jdbcTemplate.update(sql, addNoticeInfo);
	}

	public List<NoticeInfoDto> getMainSiteNoticeList() {
		int needMessageNumber = 3;
		
		String sql = "select * from notice where notice_type = ? order by notice_create_dttm limit ?";
		List<NoticeInfoDto> siteNoticeList = null;
		
		List<Map<String, Object>> list = jdbcTemplate.queryForList(sql, 
				new Object[]{NoticeType.SITE.toIntValue(), needMessageNumber} );
		
		siteNoticeList = new ArrayList<NoticeInfoDto>();
		NoticeInfoDto noticeInfo = null;
		for (Map<String, Object> map : list) {
			noticeInfo = new NoticeInfoDto((Long) map.get("notice_id"));
			
			noticeInfo.setNoticeContent(String.valueOf(map.get("notice_context")));
			noticeInfo.setNoticeSummaryText(String.valueOf(map.get("notice_summary_text")));
			noticeInfo.setNoticeCreateDttm(((Long) map.get("notice_create_dttm")).intValue() );
			noticeInfo.setNoticePicUrl(String.valueOf(map.get("notice_pic_url")));
			noticeInfo.setNoticeTitle(String.valueOf(map.get("notice_title")));
			noticeInfo.setNoticeType(NoticeType.getNoticeType((Integer) map.get("notice_type")));
			
			siteNoticeList.add(noticeInfo);
		}
		return siteNoticeList;
	}

	public List<NoticeInfoDto> getMainCommunityNoticeList() {
		int needMessageNumber = 3;
		
		String sql = "select * from notice where notice_type = ? order by notice_create_dttm limit ?";
		List<NoticeInfoDto> siteNoticeList = null;
		
		List<Map<String, Object>> list = jdbcTemplate.queryForList(sql, 
				new Object[]{NoticeType.COMMUNITY.toIntValue(), needMessageNumber} );
		
		siteNoticeList = new ArrayList<NoticeInfoDto>();
		NoticeInfoDto noticeInfo = null;
		for (Map<String, Object> map : list) {
			noticeInfo = new NoticeInfoDto((Long) map.get("notice_id"));
			
			noticeInfo.setNoticeContent(String.valueOf(map.get("notice_context")));
			noticeInfo.setNoticeSummaryText(String.valueOf(map.get("notice_summary_text")));
			noticeInfo.setNoticeCreateDttm(((Long) map.get("notice_create_dttm")).intValue() );
			noticeInfo.setNoticePicUrl(String.valueOf(map.get("notice_pic_url")));
			noticeInfo.setNoticeTitle(String.valueOf(map.get("notice_title")));
			noticeInfo.setNoticeType(NoticeType.getNoticeType((Integer) map.get("notice_type")));
			
			siteNoticeList.add(noticeInfo);
		}
		return siteNoticeList;
	}

	public List<NoticeInfoDto> getAllNoticeList() {
		String sql = "select * from notice";
		List<NoticeInfoDto> noticeList = null;
		
		List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
		noticeList = new ArrayList<NoticeInfoDto>();
		NoticeInfoDto noticeInfo = null;
		for (Map<String, Object> map : list) {
			noticeInfo = new NoticeInfoDto((Long) map.get("notice_id"));
			
			noticeInfo.setNoticeContent(String.valueOf(map.get("notice_context")));
			noticeInfo.setNoticeSummaryText(String.valueOf(map.get("notice_summary_text")));
			noticeInfo.setNoticeCreateDttm(((Long) map.get("notice_create_dttm")).intValue() );
			noticeInfo.setNoticePicUrl(String.valueOf(map.get("notice_pic_url")));
			noticeInfo.setNoticeTitle(String.valueOf(map.get("notice_title")));
			noticeInfo.setNoticeType(NoticeType.getNoticeType((Integer) map.get("notice_type")));
			
			noticeList.add(noticeInfo);
		}
		
		return noticeList;
	}
}
